package com.neulogistics.core.customercenter.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.core.customercenter.api.application.command.CreateCustomerCommand;
import com.neulogistics.core.customercenter.api.application.converter.CustomerEntityCommandConverter;
import com.neulogistics.core.customercenter.domain.model.aggregate.Customer;
import com.neulogistics.core.customercenter.domain.repository.ICustomerRepository;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CreateCustomerCommandHandler implements IRequestHandler<CreateCustomerCommand, BaseResponse<String>> {
    @Resource
    private ICustomerRepository customerRepository;


    @Override
    public BaseResponse<String> handle(CreateCustomerCommand command) {
        // 创建客户聚合根
        Customer customer = CustomerEntityCommandConverter.commandToEntity(command);

        // 持久化保存
        customerRepository.save(customer);
        return ResultUtils.success(customer.getCustomerId().toString());
    }
}
